<template>
  <el-dialog :title="title" :visible.sync="open" width="70%" :before-close="handleClose" class="dialogClass">
    <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="80px">
      <el-row :gutter="20">
        <el-col :xl="8" :lg="8">
          <el-form-item label="姓名" prop="name">
            <el-input size="big" v-model.number="ruleForm.name" placeholder="请输入客户姓名" :disabled="!controlShow"
              clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="8" :lg="8" v-if="controlShow">
          <el-form-item label="密码" prop="password">
            <el-input size="big" v-model="ruleForm.password" placeholder="请输入密码" clearable show-password
              autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="8" :lg="8" v-if="controlShow">
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input size="big" v-model="ruleForm.confirmPassword" clearable placeholder="请确认密码" show-password
              autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="8" :lg="8">
          <el-form-item label="地址" prop="address">
            <el-input size="big" v-model="ruleForm.address" placeholder="请输入地址" :disabled="!controlShow"
              clearable></el-input>
          </el-form-item>
        </el-col>
        
        <el-col :xl="8" :lg="8">
          <el-form-item label="手机号" prop="phone">
            <el-input size="big" v-model="ruleForm.phone" placeholder="请输入手机号" :disabled="!controlShow"
              clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="8" :lg="8">
          <el-form-item label="初始余额" prop="balance">
            <el-input size="big" v-model="ruleForm.balance" placeholder="请输入初始余额" :disabled="!controlShow"
              clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="24" :lg="24">
          <el-form-item label="备注" prop="remark">
            <el-input size="big" type="textarea" v-model="ruleForm.remark" placeholder="请输入备注"
              :autosize="{ minRows: 5, maxRows: 8 }" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="big" @click="onSubmit('ruleForm')" :loading="loading">{{
        submitButton
      }}</el-button>
      <el-button size="big" @click="resetForm('ruleForm')" v-if="restBtn">重置</el-button>
      <el-button size="big" @click="close('ruleForm')">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { openAccount} from '@/apis/client/clientManagement.js'

export default {
  name: "openAccount",
  data() {
    let checkTel = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('电话号码不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          callback();
        }
      }, 500);
    };
    let inputPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.confirmPassword !== '') {
          this.$refs.ruleForm.validateField('confirmPassword');
        }
        callback();
      }
    };
    let confirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      // 委派和员工的显示切换标志
      controlShow: true,
      // 重置按钮的显示切换标志
      restBtn: true,
      // 提交按钮
      submitButton: '',
      // 控制弹窗
      open: false,
      // 控制按钮加载状态
      loading: false,
      // 弹窗标题
      title: '客户开户',
      // 店铺下拉
      options: null,
      // 表单项
      ruleForm: {
        id: '',
        name: '',
        password: '',
        confirmPassword: '',
        address: '',
        phone: '',
        balance: '',
        remark: '',
      
      },
      nullruleForm: {
        id: '',
        name: '',
        password: '',
        confirmPassword: '',
        address: '',
        phone: '',
        balance: '',
        remark: '',
      },

      // 表单校验
      rules: {
        phone: [{required: true, trigger: 'blur' }],
        password: [{ validator: inputPassword, required: true, trigger: 'blur' }],
        confirmPassword: [{ validator: confirmPassword, required: true, trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        balance: [{ required: true, message: '请输入初始余额', trigger: 'blur' }],
        remark: [{ required: true, }],
      },

    }
  },
  created() {

  },
  methods: {
    // 控制弹窗

    show(title, row) {
      const _this = this
      _this.title = title;
      _this.open = true;

        _this.submitButton = "注册";
      
    },

    close(formName) {
      this.open = false;
      this.resetForm(formName);
    },
    handleClose(done) {
      const _this = this;
      _this.$confirm('系统可能不会保存您的更改', '确认关闭？')
        .then(_ => {
          done();
          _this.resetForm('ruleForm');
        })
        .catch(_ => { });
    },
    onSubmit(formName) {
      const _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
              console.log(_this.title);

            if (_this.title == '客户开户') {
              // console.log(_this.ruleForm);
              openAccount(_this.ruleForm).then(function (res) {
                if (res.code === 0) {
                  _this.$alert(_this.ruleForm.name + res.msg, '提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                      _this.close('ruleForm');
                      _this.$parent.$parent.search()
                    }
                  })
                }
              })
             } 
        }
      });
    },
    // 表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.ruleForm = this.nullruleForm
    }
  }
}
</script>
